//
//  ArtMark.m
//  SmartRoadShow
//
//  Created by  ibokan on 10-9-25.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "ArtMark.h"
#import "database.h"


@implementation ArtMark
@synthesize _id, artMark_id,news_id,_level;
@synthesize _title,_thumb,_city,_marktype,_address,_tel,_googleposi,_content,_pubdate;

+(BOOL) findArtMarkID:(int)artmarkId
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet>rs;
	
	rs = [db executeQuery:@"select artMark_id from ArtMark"];
	while ([rs next])
	{
		int artId = [[rs objectForColumn:@"artMark_id"] intValue];
		
		if(artId == artmarkId)
		{
			return YES;
		}
	}
	
	return NO;
}


+(NSArray *)findCity:(NSString *)city MarkType:(NSString *)type
{
	PLSqliteDatabase *db = [database setup];
	id<PLResultSet>rs;
	NSMutableArray *findArray = [[NSMutableArray alloc] init];
	
//	if(city == nil && type == nil)
//	{
//		NSString* sql = @"select * from ArtMark";
//		rs = [db executeQuery:sql];
//		
//	}
//	else if(city != nil && type == nil)
//	{
//		NSString *sql = nil;
//		if ([city isEqualToString:@"全部"]) {
//			sql = [NSString stringWithFormat:@"select * from ArtMark"];
//		}else {
//			sql = [NSString stringWithFormat:@"select * from ArtMark where city = '%@'",city];
//		}
//		rs = [db executeQuery:sql];
//	}
//	else if(city == nil && type != nil)
//	{
//		NSString *sql = nil;
//		if ([type isEqualToString:@"全部"]) {
//			sql = [NSString stringWithFormat:@"select * from ArtMark"];
//		}
//		else {
//			sql = [NSString stringWithFormat:@"select * from ArtMark where marktype = '%@'",type];
//		}
//		rs = [db executeQuery:sql];
//	}
//	else if(city != nil && type != nil)
//	{
	NSString *sql = nil;
	if ([city isEqualToString:@"全部"]) {
		if ([type isEqualToString:@"全部"]) {
			sql = [NSString stringWithFormat:@"select * from ArtMark"]; 
		}
		else {
			sql = [NSString stringWithFormat:@"select * from ArtMark where marktype = '%@'",type]; 
		}
		rs = [db executeQuery:sql];
	}
	else {
		if ([type isEqualToString:@"全部"]) {
			sql = [NSString stringWithFormat:@"select * from ArtMark where city = '%@'",city];
		}
		else {
			sql = [NSString stringWithFormat:@"select * from ArtMark where city = '%@' and marktype = '%@'",city,type];
		}
		//		}
		rs = [db executeQuery:sql];
	}
	while ([rs next])
	{
		int Id = [[rs objectForColumn:@"ID"] intValue];
		int artMarkId = [[rs objectForColumn:@"artMark_id"] intValue];
		int newsId = [[rs objectForColumn:@"news_id"] intValue];
		NSString *title = [rs objectForColumn:@"title"];
		NSString *thumb = [rs objectForColumn:@"thumb"];
		NSString *city = [rs objectForColumn:@"city"];
		NSString *type = [rs objectForColumn:@"marktype"];
		int level = [[rs objectForColumn:@"level"] intValue];
		NSString *address = [rs objectForColumn:@"address"];
		NSString *tel = [rs objectForColumn:@"tel"];
		NSString *posi = [rs objectForColumn:@"googleposi"];
		NSString *content = [rs objectForColumn:@"content"];
		NSString *pubdate = [rs objectForColumn:@"pubdate"];
		
		ArtMark *artMark1 = [[ArtMark alloc] initWithID:Id 
											 ArtMark_id:artMarkId 
												 NewsId:newsId 
												  Title:title
												  Thumb:thumb 
												   City:city 
											   Marktype:type 
												  Level:level 
												Address:address 
													Tel:tel
											 Googleposi:posi 
												Content:content 
												Pubdate:pubdate];
		[findArray addObject:artMark1];
		[artMark1 release];
	}
	
	[rs close];
	
	return [findArray autorelease];
}


+(int)insertWithArtMarkID:(int)markId NewsId:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Marktype:(NSString *)marktype Level:(int)level Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content Pubdate:(NSString *)pubdate
{
	PLSqliteDatabase *db = [database setup];
	BOOL re = [db executeUpdate:@"insert into ArtMark(artMark_id,news_id,title,thumb,city,marktype,level,address,tel,googleposi,content,pubdate) values(?,?,?,?,?,?,?,?,?,?,?,?)",
			   [NSNumber numberWithInt:markId],
			   [NSNumber numberWithInt:newsId],
			   title,
			   thumb,
			   city,
			   marktype,
			   [NSNumber numberWithInt:level],
			   add,
			   tel,
			   posi,
			   content,
			   pubdate];

	return re;
}

+(int)updateWithArtMarkID:(int)markId NewsId:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Marktype:(NSString *)marktype Level:(int)level Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content Pubdate:(NSString *)pubdate
{
	PLSqliteDatabase *db = [database setup];
	//NSString* sql = [NSString stringWithFormat:@"update ArtMark set news_id=%@,title=%@,thumb=%@,city=%@,marktype=%@,level=%@,address=%@,tel=%@,googleposi=%@,content=?pubdate=? where artMark_id=?"];
	
	BOOL re = [db executeUpdate:@"update ArtMark set news_id=?,title=?,thumb=?,city=?,marktype=?,level=?,address=?,tel=?,googleposi=?,content=?, pubdate=? where artMark_id=?",
			   [NSNumber numberWithInt:newsId],
			   title,
			   thumb,
			   city,
			   marktype,
			   [NSNumber numberWithInt:level],
			   add,
			   tel,
			   posi,
			   content,
			   pubdate,
			   [NSNumber numberWithInt:markId]];
	
	return re;
}


+(int)delete:(int)Id
{
	PLSqliteDatabase *db = [database setup];
	
	BOOL re = [db executeUpdate:@"delete from ArtMark where ID = ?",[NSNumber numberWithInt:Id]];
	
	return re;
}


-(id) initWithID:(int)Id ArtMark_id:(int)markId NewsId:(int)newsId Title:(NSString *)title Thumb:(NSString *)thumb City:(NSString *)city Marktype:(NSString *)marktype Level:(int)level Address:(NSString *)add Tel:(NSString *)tel Googleposi:(NSString *)posi Content:(NSString *)content Pubdate:(NSString *)pubdate
{
	if(self = [super init])
	{
		_id = Id;
		artMark_id = markId;
		news_id = newsId;
		_title = [title retain];
		_thumb = [thumb retain];
		_city = [city retain];
		_marktype = [marktype retain];
		_level = level;
		_address = [add retain];
		_tel = [tel retain];
		_googleposi = [posi retain];
		_content = [content retain];
		_pubdate = [pubdate retain];
	}
	
	return self;
}


+(NSString*)findNewsContentFromNewsId:(int)newsId{
	PLSqliteDatabase *db = [database setup];
	
	NSString* sql = [NSString stringWithFormat:@"select * from ArtMark where ID=%d", newsId];
	id<PLResultSet> rs = [db executeQuery:sql];
	
	NSString* content = nil;
	while ([rs next]) {
		content = [rs objectForColumn:@"content"];
	}
	return content;
}


@end
